﻿using TMOOC.CM.MobileShop.Entity;
using TMOOC.CM.MobileShop.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

namespace TMOOC.CM.MobileShop.Business.Base_Manage
{
    public class Base_UserLogBusiness : BaseBusiness<Base_UserLog>, IBase_UserLogBusiness, IDependency
    {
        #region 外部接口

        public List<Base_UserLog> GetDataList(Pagination pagination, string condition, string keyword)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_UserLog>();

            //筛选
            if (!condition.IsNullOrEmpty() && !keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_UserLog, bool>(
                    ParsingConfig.Default, false, $@"{condition}.Contains(@0)", keyword);
                where = where.And(newWhere);
            }

            return q.Where(where).GetPagination(pagination).ToList();
        }

        public Base_UserLog GetTheData(string id)
        {
            return GetEntity(id);
        }

        public AjaxResult AddData(Base_UserLog data)
        {
            Insert(data);

            return Success();
        }

        public AjaxResult AddData(List<Base_UserLog> data)
        {
            for (int i = 0; i < data.Count; i++)
                data[i].InitEntity();
            Insert(data);
            return Success();
        }

        public AjaxResult UpdateData(Base_UserLog data)
        {
            Update(data);

            return Success();
        }

        public AjaxResult DeleteData(List<string> ids)
        {
            Delete(ids);

            return Success();
        }

        public AjaxResult DeleteAllData()
        {

                DeleteAll();
            return Success();
        }


        public void SaveLog(string data, IBase_UserBusiness _UserBusiness)
        {
            try
            {
                var l1 = JsonConvert.DeserializeObject<List<Base_Action>>(data);
                var log = new Base_UserLog
                {
                    ViewTime = DateTime.Now,
                    ViewName = l1[0].Name,
                    ViewRoute = l1[0].Url,
                };
                log.InitEntity();
                log.UserID = log.CreatorId;
                var bu = _UserBusiness.GetTheData(log.UserID);
                log.UserName = bu.UserName;
                AddData(log);
                MLogHelper.Info(JsonConvert.SerializeObject(log),log.UserName,true);
            }
            catch(Exception ex)
            {
                MLogHelper.Error(ex.ToString() + ex.Message, "ErrorLog", true);
            }
        }
        #endregion

        #region 私有成员

        #endregion

        #region 数据模型

        #endregion
    }
}